package com.dongdongshop.service.impl;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationExample;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.vo.SpecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@DubboService
public class TbSpecificationServiceImpl implements TbSpecificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private TbSpecificationOptionMapper optionMapper;
    @Override
    public PageResult listSpecifications(Integer pageNum, Integer pageSize,String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSpecification> tbSpecifications = specificationMapper.selectAllByNameLike(name);
        PageInfo<TbSpecification> i = new PageInfo<>(tbSpecifications);
        return new PageResult(i.getPageNum(),i.getPageSize(),i.getTotal(),i.getPages(),i.getList());
    }

    @Override
    @Transactional
    public void addSpecAndOptions(TbSpecification specification, String specOption) {
        List<TbSpecificationOption> options = JSONUtil.toList(specOption, TbSpecificationOption.class);
        specificationMapper.insertSelective(specification);
        for (TbSpecificationOption o : options) {
            o.setSpecId(specification.getId());
            optionMapper.insertSelective(o);
        }
    }

    @Override
    public SpecVO selectSpecAndOptionById(Long id) {
        TbSpecification spec = specificationMapper.selectByPrimaryKey(id);
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> options = optionMapper.selectByExample(example);
        SpecVO vo = new SpecVO(spec.getId(),spec.getSpecName(),options);
        return vo;
    }

    @Override
    public void updateSpecAndSperOption(TbSpecification spec, String specOption) {
        specificationMapper.updateByPrimaryKeySelective(spec);
        List<TbSpecificationOption> options = JSONUtil.toList(specOption, TbSpecificationOption.class);
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(spec.getId());
        int i = optionMapper.deleteByExample(example);
        for (TbSpecificationOption option : options) {
            option.setSpecId(spec.getId());
            optionMapper.insertSelective(option);
        }
    }

    @Override
    public void deleteSpecs(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(idList);
        specificationMapper.deleteByExample(example);
        for (int i = 0; i < idList.size(); i++) {
            Long specId = idList.get(i);
            TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria optionCriteria = optionExample.createCriteria();
            optionCriteria.andSpecIdEqualTo(specId);
            optionMapper.deleteByExample(optionExample);
        }
    }

    @Override
    public List<TbSpecification> listSpecAll() {
        return specificationMapper.selectByExample(null);
    }
}
